/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateOrder.java
 *
 * Created on Dec 10, 2009, 12:08:05 PM
 */

package GUI;

import Business.BillDetails_BS;
import Business.Bill_BS;
import Business.CustomerProcess_BS;
import Business.Customer_BS;
import Business.ProductProcess_BS;
import Business.Product_BS;
import Business.BillDetailsTableModel;
import Business.BillProcess_BS;
import DAO.Bill_DAO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author trangnn_b00557
 */
public class CreateBill extends javax.swing.JDialog {
    CustomerProcess_BS customerBO = new CustomerProcess_BS();
    ProductProcess_BS productBO = new ProductProcess_BS();
    BillDetailsTableModel detailsTableModel;
    private BillDetails_BS item;
    private Bill_BS b = new Bill_BS();
    Vector<Product_BS> products;

//    private int newId;
    /** Creates new form CreateOrder */
    public CreateBill(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Vector<Customer_BS> customers = new Vector<Customer_BS>(customerBO.getAllCustomer());
        cbxCustomer.setModel(new DefaultComboBoxModel(customers));

        products = new Vector<Product_BS>(productBO.getAllProducts());
        cbxProduct.setModel(new DefaultComboBoxModel(products));

        detailsTableModel = new BillDetailsTableModel();
        tbProductList.setModel(detailsTableModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        lblCodeCustomer = new javax.swing.JLabel();
        lblDeliveryDate = new javax.swing.JLabel();
        txtDeliveryDate = new javax.swing.JTextField();
        lblDeliveryAddress = new javax.swing.JLabel();
        txtDeleveryAddress = new javax.swing.JTextField();
        cbxCustomer = new javax.swing.JComboBox();
        btCreateOrder = new javax.swing.JButton();
        btClose = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        lblCode2 = new javax.swing.JLabel();
        cbxProduct = new javax.swing.JComboBox();
        lblCode3 = new javax.swing.JLabel();
        txtQuantity = new javax.swing.JTextField();
        btAddProductItems = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbProductList = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        lblPay = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        txtTotal = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtPay = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btPay = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtBalance = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtDiscount = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtTotalPrice = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        btTotalPrice = new javax.swing.JButton();
        btRemove = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Create Order");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Create Bill", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Monotype Corsiva", 1, 36), new java.awt.Color(51, 102, 0))); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Customer Information"));

        lblCodeCustomer.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblCodeCustomer.setText("Customer Name : ");

        lblDeliveryDate.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblDeliveryDate.setText("Delivery Date : ");

        lblDeliveryAddress.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblDeliveryAddress.setText("Delivery Address : ");

        cbxCustomer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbxCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxCustomerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(152, 152, 152)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblDeliveryAddress)
                    .addComponent(lblDeliveryDate)
                    .addComponent(lblCodeCustomer))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtDeleveryAddress)
                    .addComponent(cbxCustomer, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtDeliveryDate, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(230, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodeCustomer)
                    .addComponent(cbxCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblDeliveryDate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblDeliveryAddress))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(txtDeliveryDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDeleveryAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        btCreateOrder.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Add.png"))); // NOI18N
        btCreateOrder.setText("Save");
        btCreateOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCreateOrderActionPerformed(evt);
            }
        });

        btClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Close-icon.png"))); // NOI18N
        btClose.setText("Close");
        btClose.setActionCommand("buttton1");
        btClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCloseActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Add Product")));

        lblCode2.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblCode2.setText("Product:");

        cbxProduct.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        lblCode3.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblCode3.setText("Quantity:");

        btAddProductItems.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Add-icon.png"))); // NOI18N
        btAddProductItems.setText("Add");
        btAddProductItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddProductItemsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(lblCode2)
                .addGap(30, 30, 30)
                .addComponent(cbxProduct, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(54, 54, 54)
                .addComponent(lblCode3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addComponent(btAddProductItems)
                .addGap(33, 33, 33))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(lblCode2)
                .addComponent(cbxProduct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lblCode3)
                .addComponent(btAddProductItems))
        );

        tbProductList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbProductList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbProductListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbProductList);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Pay"));

        lblPay.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPay.setText("Pay : ");

        lblTotal.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblTotal.setText("Total : ");

        txtTotal.setText("0");

        jLabel1.setText("$");

        txtPay.setText("0");

        jLabel2.setText("$");

        btPay.setText("Pay");
        btPay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPayActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Balance : ");

        txtBalance.setText("0");

        jLabel4.setText("$");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Discount : ");

        txtDiscount.setText("0");

        jLabel8.setText("%");

        txtTotalPrice.setText("0");

        jLabel11.setText("$");

        btTotalPrice.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Add.png"))); // NOI18N
        btTotalPrice.setText("TotalPrice");
        btTotalPrice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTotalPriceActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(lblTotal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTotal)
                        .addGap(47, 47, 47)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel8))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(lblPay)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtPay, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addGap(57, 57, 57)
                        .addComponent(btPay)))
                .addGap(24, 24, 24)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtBalance, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btTotalPrice))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTotalPrice, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel11)
                .addGap(103, 103, 103))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTotal)
                    .addComponent(txtTotal)
                    .addComponent(btTotalPrice)
                    .addComponent(jLabel1)
                    .addComponent(jLabel7)
                    .addComponent(txtDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel11)
                    .addComponent(txtTotalPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btPay)
                    .addComponent(lblPay)
                    .addComponent(txtPay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtBalance)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)))
        );

        btRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Delete-icon.png"))); // NOI18N
        btRemove.setText("Remove");
        btRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(464, Short.MAX_VALUE)
                .addComponent(btCreateOrder)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btClose)
                .addGap(52, 52, 52))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(551, Short.MAX_VALUE)
                .addComponent(btRemove)
                .addGap(36, 36, 36))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 678, Short.MAX_VALUE)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btRemove)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btClose)
                    .addComponent(btCreateOrder, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btAddProductItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddProductItemsActionPerformed
        // TODO add your handling code here:
        if (txtQuantity.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Please enter quantity");
            return;
        }
        Product_BS product = (Product_BS) cbxProduct.getSelectedItem();
        int qua = 0;
        int quantity = Integer.parseInt(txtQuantity.getText());
        if (item == null) {
            item = new BillDetails_BS(product.getpCode(), product.getpName(), product.getpPrice(), quantity);
        } else {
            if (product.getpName().equals(item.getPName().trim())) {
                qua = item.getBdQuantity() + quantity;
                item.setBdQuantity(qua);

            } else {
                item = new BillDetails_BS(product.getpCode(), product.getpName(), product.getpPrice(), quantity);

            }
        }
        detailsTableModel.addOrderItem(item);

        float total=0;
        //float balance=totalPrice;
        int row= tbProductList.getRowCount();
        for(int i=0; i<row; i++) {
            Float subPrice= (Float)(tbProductList.getValueAt(i,4));
//            System.out.println(tbProductList.getValueAt(i,4));

            total += subPrice;
           // balance = totalPrice;
        }
        //b.setTotal(totalPrice);
        //b.setBUnpaid(Float.valueOf(txtBalance.getText()));
       //txtBalance.setText(String.valueOf(balance));
        txtTotal.setText(String.valueOf(total));
}//GEN-LAST:event_btAddProductItemsActionPerformed

    private void btPayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPayActionPerformed
        // TODO add your handling code here:
        float pay = Float.valueOf(txtPay.getText().trim()).floatValue();
        float totalPrice= b.getTotal();
        float balance= b.getBUnpaid();
        if(pay !=0) {
            if(pay > totalPrice) {
                JOptionPane.showMessageDialog(this,"Extra Money : " +(pay - totalPrice)+ " $");
                balance = 0;
                b.setBPaid(totalPrice);
                b.setBUnpaid(balance);
            } else{
                balance = totalPrice - pay;
                b.setBPaid(pay);
                b.setBUnpaid(balance);
            }
        } else {
            balance= totalPrice;
            b.setBPaid(balance);
            b.setBUnpaid(balance);
        }

        txtBalance.setText(String.valueOf(balance));
}//GEN-LAST:event_btPayActionPerformed

    private void btRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoveActionPerformed
        // TODO add your handling code here:
        float total= Float.valueOf(txtTotal.getText());
        int row = tbProductList.getSelectedRow();
        Float subTotal= (Float)tbProductList.getValueAt(row,4);
        if (JOptionPane.showConfirmDialog(this, "Are you sure you want to remove this product?")
                == JOptionPane.YES_OPTION) {

            BillDetails_BS billDetails = getProductOrderAtRow(row);
            Bill_DAO.remove(billDetails.getPID());

            ((BillDetailsTableModel) tbProductList.getModel()).removeOrder(billDetails);

            total -= subTotal;
            //ps.setTotal(total);
            txtTotal.setText(String.valueOf(total));
        }
    }//GEN-LAST:event_btRemoveActionPerformed

    private void btTotalPriceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTotalPriceActionPerformed
        // TODO add your handling code here:
        float discount = Float.valueOf(txtDiscount.getText());
        float total = Float.valueOf(txtTotal.getText());
        float totalPrice =0;
        float balance=totalPrice;
        if(discount < 0){
            JOptionPane.showMessageDialog(this, "Discount isn't ... 0");
        }
        else if(discount >= 100){
            JOptionPane.showMessageDialog(this, "Discount isn't  more than 100%");
        }
        else{
            totalPrice = total - (total * discount)/100;
            balance = totalPrice;
        }
        b.setTotal(totalPrice);
        b.setBUnpaid(Float.valueOf(txtBalance.getText()));
        txtBalance.setText(String.valueOf(balance));
        txtTotalPrice.setText(String.valueOf(totalPrice));
    }//GEN-LAST:event_btTotalPriceActionPerformed

    private void tbProductListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbProductListMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() > 1) {
            int row = tbProductList.getSelectedRow();
        }
    }//GEN-LAST:event_tbProductListMouseClicked

    private void btCreateOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCreateOrderActionPerformed
        // TODO add your handling code here:
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
        if (txtDeliveryDate.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Please enter Delivery Date","Error",JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (txtDeliveryDate.getText().trim().length() != dateFormat.toPattern().length()){
            JOptionPane.showMessageDialog(this, "Please enter correct date format MM/DD/YYYY","Error",JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (txtDeleveryAddress.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Please enter Delivery Address","Error",JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (txtQuantity.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Please enter quantity","Error",JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(tbProductList.getRowCount() == 0)
        {
            JOptionPane.showMessageDialog(this, "Please add product","Error",JOptionPane.ERROR_MESSAGE);
            return;
        }
        BillProcess_BS op = BillProcess_BS.getOrder();
        Bill_DAO oda = new Bill_DAO();

        Calendar ca = Calendar.getInstance();
        Date today = new Date();
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
        String orDate = format.format(today.getTime());

        Date deliveryDate = null;
        try {
            deliveryDate = format.parse(txtDeliveryDate.getText());
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
//        Date date = new Date();
//        date = format.parse(deliveryDate, null);
        String deliveryAddress = txtDeleveryAddress.getText();

        Customer_BS customer = (Customer_BS) cbxCustomer.getSelectedItem();
        int id = customer.getcID();
        float pay=0;
        pay = Float.valueOf(txtPay.getText().trim()).floatValue();
        float balance = Float.valueOf(txtBalance.getText().trim()).floatValue();
        float discount = Float.valueOf(txtDiscount.getText().trim()).floatValue();

        int bStatus;
        if(pay == 0)
        {
            bStatus = 0;
            b = new Bill_BS(id, deliveryDate, deliveryAddress, discount, pay, balance, orDate, bStatus);
        }
        else if(balance ==0)
        {
           bStatus = 2;
           b = new Bill_BS(id, deliveryDate, deliveryAddress, discount, pay, balance, orDate, bStatus);
        }
        else
        {
            bStatus = 1;
            b = new Bill_BS(id, deliveryDate, deliveryAddress, discount, pay, balance, orDate, bStatus);
        }
        int newID = op.addOrder(b);

        int row = (int)tbProductList.getRowCount();
        boolean success = false;
        for(int i=0; i<row; i++)
        {
            Integer pid=(Integer)tbProductList.getValueAt(i, 0) ;
            Float price= (Float)tbProductList.getValueAt(i, 2);
            Integer quantity= (Integer)tbProductList.getValueAt(i, 3);

            BillDetails_BS b1= new BillDetails_BS(newID, pid, price , quantity, orDate);
            success = op.addOrderDetails(b1);

            BillDetails_BS b2 = new BillDetails_BS(pid,quantity);
//            oda.updateBuyQuantity(b2);
            if(!oda.updateBuyQuantity(b2)) {
                JOptionPane.showMessageDialog(this, "Cannot buy this quantity of product");
                return;
            }
        }
        if(success = false)
        {
            JOptionPane.showMessageDialog(this,"Insert Error!", "Error", JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            JOptionPane.showMessageDialog(this,"Insert Successfull", "Message",JOptionPane.DEFAULT_OPTION);
            this.dispose();
        }

    }//GEN-LAST:event_btCreateOrderActionPerformed

    private void cbxCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxCustomerActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbxCustomerActionPerformed

    private void btCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCloseActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btCloseActionPerformed

    /**
    * @param args the command line arguments
    */
private BillDetails_BS getProductOrderAtRow(int rowIndex) {
        BillDetailsTableModel model = (BillDetailsTableModel) tbProductList.getModel();
        return model.getProduct(rowIndex);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddProductItems;
    private javax.swing.JButton btClose;
    private javax.swing.JButton btCreateOrder;
    private javax.swing.JButton btPay;
    private javax.swing.JButton btRemove;
    private javax.swing.JButton btTotalPrice;
    private javax.swing.JComboBox cbxCustomer;
    private javax.swing.JComboBox cbxProduct;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCode2;
    private javax.swing.JLabel lblCode3;
    private javax.swing.JLabel lblCodeCustomer;
    private javax.swing.JLabel lblDeliveryAddress;
    private javax.swing.JLabel lblDeliveryDate;
    private javax.swing.JLabel lblPay;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JTable tbProductList;
    private javax.swing.JLabel txtBalance;
    private javax.swing.JTextField txtDeleveryAddress;
    private javax.swing.JTextField txtDeliveryDate;
    private javax.swing.JTextField txtDiscount;
    private javax.swing.JTextField txtPay;
    private javax.swing.JTextField txtQuantity;
    private javax.swing.JLabel txtTotal;
    private javax.swing.JLabel txtTotalPrice;
    // End of variables declaration//GEN-END:variables

}
